package com.biwin.module.org.starter.controller;

import com.biwin.common.app.basic.BwQueryDto;
import com.biwin.common.app.exception.BwBasicException;
import com.biwin.common.app.exception.BwNotFoundException;
import com.biwin.core.main.controller.BwBasicController;
import com.biwin.module.org.api.dto.PositionDto;
import com.biwin.module.org.api.enums.BwPositionExceptionEnum;
import com.biwin.module.org.api.service.IPositionService;
import com.biwin.module.org.api.vo.PositionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Position Controller
 *
 * @author biwin
 * @since 0.0.1
 */
@Api(tags = "岗位管理")
@RestController("Position Controller")
@RequestMapping("/api/auth/position")
public class PositionController extends BwBasicController {
    private static final Logger log = LoggerFactory.getLogger(SystemUserController.class);

    @Autowired
    private IPositionService positionService;

    @ApiOperation(value = "岗位列表", httpMethod = "POST")
    @PostMapping("/list")
    public void queryList(@RequestBody @Validated BwQueryDto queryDto) {}

    @ApiOperation(value = "添加岗位", httpMethod = "PUT")
    @PutMapping("/add")
    public PositionVo add(@RequestBody @Validated PositionDto positionDto) {
        try {
            positionService.findByName(positionDto.getName());
        } catch (BwNotFoundException e) {
            return positionService.add(positionDto);
        }
        throw new BwBasicException(BwPositionExceptionEnum.POSITION_EXIST);
    }

    @ApiOperation(value = "修改岗位", httpMethod = "POST")
    @PostMapping("/update")
    public PositionVo update(@RequestBody @Validated PositionDto positionDto) {
        Assert.notNull(positionDto.getId(), "id must be not null");
        Assert.notNull(positionDto.getVersion(), "version must be not null");
        return positionService.update(positionDto);
    }

    @ApiOperation(value = "删除岗位", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable(name = "id") Long id) {
        positionService.delete(id);
    }

    @ApiOperation(value = "禁用岗位", httpMethod = "POST")
    @PostMapping("/disable/{id}")
    public void disable(@PathVariable(name = "id") Long id) {
        positionService.disable(id);
    }

    @ApiOperation(value = "启用岗位", httpMethod = "POST")
    @PostMapping("/enable/{id}")
    public void enable(@PathVariable(name = "id") Long id) {
        positionService.enable(id);
    }
}
